<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notepad</title>

    <!-- mui -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css">

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- bootstrap-validate -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css">

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- 重置样式 -->
    <link rel="stylesheet/less" href="./assets/css/reset.less" />

    <!-- 公共样式 -->
    <link rel="stylesheet/less" href="./assets/css/common.less" />

    <!-- 页面独立的 -->
    <link rel="stylesheet/less" href="./assets/css/UserLogin.less" />
</head>

<body>
    <!-- 用户信息 -->
    <div class="user">
        <div class="avatar wow fadeInUp">
            <img src="./assets/images/avatar.png" />
        </div>

    </div>

    <form id="register" class="login wow fadeInUp" data-wow-delay="200ms">
        <div class="form-group">
            <label for="mobile">手机号</label>
            <input type="text" class="form-control" name="mobile" id="mobile" placeholder="请输入手机号" />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" name="password" id="password" placeholder="请输入密码" />
        </div>

        <div class="action">
            <button class="btn">注册</button>
        </div>

        <div class="action">
            <a href="login.html" class="btn">登录</a>
        </div>
    </form>
</body>

</html>
<!-- 解析less的文件 -->
<script src="./assets/js/less.min.js"></script>

<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- mui -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap-validator -->
<script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>

<!-- axios -->
<script src="./assets/plugins/axios/axios.min.js"></script>

<!-- 用axios封装的request -->
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<script>
    // 实例化wow.js
    new WOW().init();

    // 防止用户重复登录
    (async () => {
        let business = cookie.get('business') ? JSON.parse(cookie.get('business')) : {}
        if (Object.getOwnPropertyNames(business).length > 0) {
            let params = {
                id: business.id,
                mobile: business.mobile
            }
            // 将id、mobile发送到business/check接口
            let res = await POST({
                url: 'business/check',
                params
            })
            if (res.code === 1) {
                // 更新cookie
                cookie.set('business', JSON.stringify(business))
                mui.toast('您已登录，无需重复登录')
                setTimeout(() => {
                    location.href = 'UserIndex.html'
                }, 2000)
            }
        }
    })();

    // 实例化bootstrap-validator验证表单
    $('#register').bootstrapValidator({
        // 填写配置项
        message: '表单内容不能为空',// 所有字段默认错误信息
        // 配置三种状态的图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invaild: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        // 设置需要验证字段
        fields: {
            mobile: {
                // 自定义验证器
                validators: {
                    // 设置该字段不能为空
                    notEmpty: {
                        message: '手机号不能为空'
                    },
                    // 设置该字段的长度
                    stringLength: {
                        min: 11,// 最小值
                        max: 11,// 最大值
                        message: '手机号必须11位'
                    },
                    // 手机号正则  输入手机号必须符合当前规则
                    regexp: {
                        // 根据工信部最新发布的手机号要求
                        regexp: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
                        message: '手机号错误'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv', function (e) {
        // 阻止多次提交
        e.preventDefault();
    });

    // 表单提交事件
    $('#register').on('submit', async function () {
        // 获取表单的某个属性
        let bootstrapValidators = $('#register').data('bootstrapValidator');

        // 判断输入内容是否验证失败
        if (!bootstrapValidators.isValid()) {
            // 验证失败就不执行后面代码
            return false;
        }

        // 组装请求参数
        let params = {
            mobile: $.trim($('#mobile').val()),
            password: $.trim($('#password').val())
        }

        // console.log(params)
        // 请求接口
        let result = await POST({
            url: 'business/register',
            params
        })

        // 判断是否注册成功
        if (result.code === 1) {
            // 提示信息
            mui.toast(result.msg)
            // 注册成功自动跳转到登录页
            setTimeout(() => {
                location.href = 'login.html'
            }, 2000)
        } else {
            // 提示信息
            mui.toast(result.msg)
        }
    })
</script>